package testLambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * lambda的 filter+map 测试
 * 
 * lambda forach 普通写法性能好2倍
 * 
 * @author jinmiao
 * 2016年7月10日
 *
 */
public class Lambda_Filter_Map_test {
	public static void main(String[] args) {
		List<Integer> list = Arrays.asList(1,2,3,4,5,7,8,9,10);
//		list = list.stream().filter(id->(id<3)).collect(Collectors.toList());
//		list.forEach(id->{System.out.println(id);});
		
		lambda(list);
		normal(list);
		
		//性能测试
		for(int i =0;i<30000000;i++)
		{
			lambda(list);
			normal(list);
		}
		long start = System.currentTimeMillis();
		for(int i =0;i<10000000;i++)
		{
			lambda(list);
		}
		System.out.println(System.currentTimeMillis()-start);
		start = System.currentTimeMillis();
		for(int i =0;i<10000000;i++)
		{
			normal(list);
		}
		System.out.println(System.currentTimeMillis()-start);
		
	}
	
	
	private static void lambda(List<Integer> list)
	{
		list = list.stream().map(id->{return id;}).filter(id->(id>5)).collect(Collectors.toList());
//		System.out.println(list.hashCode());
//		list.forEach(id->{id.intValue();});
//		System.out.println(list.hashCode());
//		list.forEach(id->{System.out.println(id);});
	}
	
	private static void normal(List<Integer> list)
	{
		List<Integer> res = new ArrayList<>(list.size());
		list.forEach(id->{
			if(id<=5)
				return;
			res.add(id);});
		
//		res.forEach(id->{System.out.println(id);});
	}
}
